package com.google.loader;

import com.alibaba.druid.filter.logging.Log4jFilter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

import java.io.IOException;
import java.util.Properties;

@Configuration
@PropertySource("classpath:db.properties")
public class db {
    @Bean(name="statFilter")
    public StatFilter StatFilter(){
        StatFilter statFilter=new StatFilter();
        statFilter.setMergeSql(true);
        statFilter.setSlowSqlMillis(1000);
        statFilter.setLogSlowSql(true);
        return statFilter;
    }

    @Bean(name="logFilter")
    public Log4jFilter LogFilter(){
        Log4jFilter log4jFilter=new Log4jFilter();
        log4jFilter.setResultSetLogEnabled(true);
        log4jFilter.setStatementLogEnabled(true);
        log4jFilter.setStatementExecutableSqlLogEnable(true);
        return log4jFilter;
    }

    @Value("${mysql.datasource.url}") String url;
    @Value("${mysql.datasource.username}") String username;
    @Value("${mysql.datasource.password}") String password;
    @Value("${mysql.datasource.driver}") String driver;
    @Value("${mysql.datasource.maxActive}") int maxActive;

    private DruidDataSource db01;

    @Bean(name = "db01")
    public DruidDataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setMaxActive(maxActive);
        db01=dataSource;
        return dataSource;
    }


    @Bean(name = "DbSF")
    @Primary
    public SessionFactory DbSF(){
        LocalSessionFactoryBean SessionFactoryBean=new LocalSessionFactoryBean();
        try {
            SessionFactoryBean.setDataSource(db01);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SessionFactoryBean.setPackagesToScan("com.longrise.entity");

        Properties properties=new Properties();
        properties.setProperty("hibernate.current_session_context_class","thread");
        properties.setProperty("hibernate.show_sql","true");
        properties.setProperty("hibernate.hbm2ddl.auto","update");
        properties.setProperty("hibernate.connection.url",url);
        properties.setProperty("hibernate.connection.driver_class","org.postgresql.Driver");

        SessionFactoryBean.setHibernateProperties(properties);
        try {
            SessionFactoryBean.afterPropertiesSet();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return SessionFactoryBean.getObject();
    }


}